//
//  ListController.m
//  iYouGou
//
//  Created by qianfeng on 15/9/18.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "ListController.h"
#import "HttpRequestManager.h"
#import "BsmallCell.h"
#import "UIImageView+AFNetworking.h"
#import "MJRefresh.h"
#import "BlongCell.h"
#import "MBProgressHUD.h"
#import "Helper.h"
@interface ListController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,MJRefreshBaseViewDelegate>
{
    int _currentPage;
    int _ident;
    NSMutableArray*_dataArry;
    MJRefreshFooterView*_footView;
    BOOL  _isTrue;
    BOOL  _isAmount;
    BOOL _isPrice;
    BOOL _isDiscount;
    BOOL _isNew;
    NSString *_amount;
   
    

}
@property (weak, nonatomic) IBOutlet UIImageView *imgView1;
@property (weak, nonatomic) IBOutlet UILabel *runLbl;
@property (weak, nonatomic) IBOutlet UILabel *nameLbl;
@property (weak, nonatomic) IBOutlet UIView *backView;

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (weak, nonatomic) IBOutlet UIImageView *imgView2;
@property (weak, nonatomic) IBOutlet UIImageView *imgView3;
@property (weak, nonatomic) IBOutlet UIImageView *imgView4;

@end

@implementation ListController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _currentPage=1;
        _ident=1;
        _dataArry=[[NSMutableArray alloc]init];
        _isTrue=YES;
        _isAmount=YES;
        _isPrice=YES;
         _isDiscount=YES;
        _isNew=YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self  initFootView];
    [self loadDataFromNet];
    
}
-(void)initFootView
{
    _footView =[[MJRefreshFooterView alloc]init];
    _footView.scrollView=_myCollectionView;
    _footView.delegate=self;
    [_myCollectionView registerNib:[UINib nibWithNibName:@"BlongCell" bundle:nil] forCellWithReuseIdentifier:@"BlongCell"];
      _nameLbl.text=_item.name;
    _runLbl.hidden=YES;
}

-(void)setThetitle
{
    _nameLbl.hidden=YES;
    _runLbl.hidden=NO;
    NSString*title=[NSString stringWithFormat:@"%@,(共%@...",_item.name,_amount];
    CGFloat width=[Helper widthOfString:title font:[UIFont systemFontOfSize:14] height:20];
    _runLbl.text=title;
    CGFloat time=(width+self.backView.frame.size.width)*0.02;
    self.runLbl.frame=CGRectMake(self.backView.frame.size.width, self.runLbl.frame.origin.y, width, 20);
    self.backView.clipsToBounds=YES;
    [UIView animateWithDuration:time animations:^{
        self.runLbl.frame=CGRectMake(self.runLbl.frame.size.width*-1, self.runLbl.frame.origin.y, width, 20);
    }completion:^(BOOL finished) {
        [self setThetitle];
    }
     ];
   

}


-(void)loadDataFromNet
{
    //开始搜索转圈
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HttpRequestManager shareInstance]getListDataByItemID:_item.itemId Andidentify:_ident Andpage:_currentPage Success:^(id responseobject) {
        //搜索完成 转圈完成
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [_dataArry addObjectsFromArray:responseobject[@"productlist_pictext"]];
        _amount=responseobject[@"account"];
        [_myCollectionView reloadData];
        if (_ident==1) {
            [self setThetitle];
        }
        
    } Failure:^(NSError *error) {
        NSLog(@"请求数据失败error===%@",error);
    }];

}


#pragma UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  _dataArry.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
      NSDictionary*dic=_dataArry[indexPath.item];
    if (_isTrue) {
     
    BsmallCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"BsmallCell" forIndexPath:indexPath];
    [cell.imgView setImageWithURL:[NSURL URLWithString:dic[@"pic"]]];
    cell.desc.text=dic[@"name"];
    cell.currentPrice.text=[NSString stringWithFormat:@"￥%@",dic[@"price2"][@"value"]];
    cell.price.text=[NSString stringWithFormat:@"￥%@",dic[@"price1"][@"value"]];
    if ([dic[@"activity_desc"] isEqualToString:@"立减"]) {
        [cell.backImage setImage:[UIImage imageNamed:@"03_img_side__9"]];
    }
    else if ([dic[@"activity_desc"] isEqualToString:@"限时抢"]) {
        [cell.backImage setImage:[UIImage imageNamed:@"03_img_side__2"]];
    }
    else  {
        cell.backImage.image=nil;
    }
        return cell;
    }
    
    // 如果点击的BOOL以后 就会变
    BlongCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"BlongCell" forIndexPath:indexPath];
     [cell.imgView setImageWithURL:[NSURL URLWithString:dic[@"pic"]]];
    cell.DescLbl.text=dic[@"name"];
    cell.currentPrice.text=[NSString stringWithFormat:@"￥%@",dic[@"price2"][@"value"]];
    cell.priceLbl.text=[NSString stringWithFormat:@"￥%@",dic[@"price1"][@"value"]];
    if ([dic[@"activity_desc"] isEqualToString:@"立减"]) {
        [cell.backimageView setImage:[UIImage imageNamed:@"03_img_side__9"]];
    }
    else if ([dic[@"activity_desc"] isEqualToString:@"限时抢"]) {
        [cell.backimageView setImage:[UIImage imageNamed:@"03_img_side__2"]];
    }
    else  {
        cell.backimageView.image=nil;
    }
    
    return cell;


}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_isTrue) {
        return CGSizeMake(150, 200);
    }
    return CGSizeMake(320, 110);

}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    
}


-(void)dealloc
{
    [_footView free];
}
#pragma MJRefreshBaseViewDelegate
-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    _currentPage++;
    [self performSelector:@selector(refreshEnd:) withObject:refreshView afterDelay:2.0];

}
-(void)refreshEnd:(MJRefreshBaseView*)refreshView
{
    [refreshView endRefreshing];
    [self loadDataFromNet];

}





- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (IBAction)changeThelist:(id)sender {
    if (_isTrue) {
        UIButton*btn=(UIButton*)sender;
        [btn setTitle:@"大图" forState:UIControlStateNormal];
        
    }
    else
    {
        UIButton*btn=(UIButton*)sender;
        [btn setTitle:@"列表" forState:UIControlStateNormal];
    
    }
    _isTrue=!_isTrue;
    [_myCollectionView reloadData];
}

- (IBAction)changTheWayToshow1:(id)sender {
    if (_isAmount) {
        _imgView1.image=[UIImage imageNamed:@"02_arrow_1"];
        _isAmount=!_isAmount;
        _ident=2;
        [_dataArry removeAllObjects];
        [self loadDataFromNet];
        
    }
    else{
      _imgView1.image=[UIImage imageNamed:@"02_arrow_2"];
     _isAmount=!_isAmount;
        _ident=1;
        [_dataArry removeAllObjects];
        [self loadDataFromNet];
    }
    
}

- (IBAction)changTheWayToshow2:(id)sender {
    if (_isPrice) {
        _imgView2.image=[UIImage imageNamed:@"02_arrow_2"];
        _isPrice=!_isPrice;
        _ident=3;
        [_dataArry removeAllObjects];
        [self loadDataFromNet];
    }
     else{
    _imgView2.image=[UIImage imageNamed:@"02_arrow_1"];
    _isPrice=!_isPrice;
         _ident=4;
         [_dataArry removeAllObjects];
         [self loadDataFromNet];
     }
    
}
- (IBAction)changTheWayToshow3:(id)sender {
    if (_isDiscount) {
        _imgView3.image=[UIImage imageNamed:@"02_arrow_2"];
        _isDiscount=!_isDiscount;
        _ident=5;
        [_dataArry removeAllObjects];
        [self loadDataFromNet];
    }
     else{
    _imgView3.image=[UIImage imageNamed:@"02_arrow_1"];
    _isDiscount=!_isDiscount;
         _ident=6;
         [_dataArry removeAllObjects];
         [self loadDataFromNet];
     }
    
}

- (IBAction)changTheWayToshow4:(id)sender {
    if (_isNew) {
        _imgView4.image=[UIImage imageNamed:@"02_arrow_2"];
        _isNew=!_isNew;
        _ident=7;
        [_dataArry removeAllObjects];
        [self loadDataFromNet];
    }
    else{
    _imgView4.image=[UIImage imageNamed:@"02_arrow_1"];
    _isNew=!_isNew;
        _ident=8;
        [_dataArry removeAllObjects];
        [self loadDataFromNet];
    }
}

@end
